package com.ka.magicexplorer.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ka.magicexplorer.activity.R;
import com.ka.magicexplorer.model.FileSuper;
import com.ka.magicexplorer.utils.ImageFetcher;

public class AdapterGallery extends BaseAdapter{

	protected List<FileSuper> listData;
	protected Context context;
	protected ImageFetcher imgc;
	private int mImageThumbSize;
	
	public AdapterGallery(Context ct, List<FileSuper> content) {
		listData = content;
		context = ct;
		mImageThumbSize = ct.getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
		imgc = new ImageFetcher(ct, mImageThumbSize);
	}
	
	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int arg0) {
		return listData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		
		if( arg1 == null){
			LayoutInflater inflat =  LayoutInflater.from(arg2.getContext());
			arg1 = inflat.inflate(R.layout.item_grid_gallery, null);
		}
		
		TextView tv = (TextView)arg1.findViewById(R.id.gallery_title);
		tv.setText(listData.get(arg0).getName());
		
		ImageView imv = (ImageView)arg1.findViewById(R.id.gallery_icon);
		
		//imgc.loadBitmap(context, listData.get(arg0).getPath(), imv);
		imgc.loadImage(listData.get(arg0).getPath(), imv);
		return arg1;
	}

    public void actionResume() {
    	imgc.setExitTasksEarly(false);
    }

    public void actionPause() {
    	imgc.setPauseWork(false);
    	imgc.setExitTasksEarly(true);
    	imgc.flushCache();
    }

    public void actionDestroy() {
    	imgc.closeCache();
    }

}
